require "Common/define"
require "Logic/Config"
require "Common/functions"

RedDotsHeroRankHandler ={};
local this = RedDotsHeroRankHandler;

local rankConfig = nil;

function RedDotsHeroRankHandler.init()
    CloseEvent(Config.EventType.Fresh_Bag,this.update);

    ListenEvent(Config.EventType.Fresh_Bag,this.update);

    if rankConfig==nil then
        rankConfig =G.dataTable["t_herorank"];
    end


    return this;
end

--参数1：指定英雄Id，nil代表检查所有英雄
---英雄详情突破页签红点
function RedDotsHeroRankHandler.getFlag(...)
    local flag = Config.RedDotsFlagState.None;
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.HeroRank,false) then
        local partnerId = ...;
        local heroList = nil;
        if partnerId~=nil then
            heroList ={};
            local hero = logicMgr.HeroManager.getHeroDatabyPartnerId(partnerId,true);
            if hero~=nil then
                table.insert(heroList,hero);
            end
        else
            heroList = logicMgr.HeroManager.GetAllHero(true);
        end
        for i = 1, #heroList do
            flag = this.getFlagByHero(heroList[i]);
            if flag == Config.RedDotsFlagState.Needful then
                break;
            end
        end
    end
    return flag;
end

function RedDotsHeroRankHandler.getFlagByHero(hero)
    local flag = Config.RedDotsFlagState.None;
    local starId = hero.partnerModelId* 1000+hero.star;
    local config =rankConfig[tostring(starId)];
    local sameNum = ItemManager.getItemCountbyModelId(hero.storeId)
    if config.f_RankUpCost ~= 0 and sameNum ~= nil and sameNum >= config.f_RankUpCost then
        flag = Config.RedDotsFlagState.Needful;
    end
    return flag;
end

function RedDotsHeroRankHandler.update()
    RedDotsManager.addBroadcast(Config.RedDotsType.HeroRank);
end